import { useAppSelector } from '@/store';
import { Popup, Tabs, Toast } from 'antd-mobile';
import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import Icon from '../Icon';
import {
  delUserChannelAction,
  getAllChannelAction,
  getUserChannelAction,
  setChannelIdAction,
  setUserChannelAction,
} from '@/store/action/channel';
import styles from './index.module.scss';

import { differenceBy } from 'lodash';
import { ChannelType } from '@/store/reducer/channel';
import history from '@/utils/history';
export default function Channel() {
  const dispatch = useDispatch();
  useEffect(() => {
    dispatch(getAllChannelAction());
    dispatch(getUserChannelAction());
  }, [dispatch]);

  const [editable, setEditable] = useState(false);
  const [visible, setVisible] = useState(false);

  const { userList, allList, channel_id } = useAppSelector((state) => state.channel);
  const restList = differenceBy(allList, userList, 'id');

  const hanelSetChannel = async (item: ChannelType) => {
    await dispatch(setUserChannelAction(item));
    Toast.show({ content: '更新成功' });
  };

  const handleDelChannel = async (id: number) => {
    if (userList.length <= 5) return Toast.show({ content: '至少保留5个' });
    if (id === 0) return Toast.show({ content: '不可删除推荐频道' });

    await dispatch(delUserChannelAction(id));
    Toast.show({ content: '删除成功' });
  };

  const handleClose = () => {
    setVisible(false);
    setEditable(false);
  };

  return (
    <div className={styles.root}>
      <Tabs
        className="tabs"
        // 4. 修改传给Tabs的类型， 以及dispatch调用时的类型
        activeKey={String(channel_id)}
        onChange={(key) => dispatch(setChannelIdAction(Number(key)))}
      >
        {userList.map((item) => (
          <Tabs.Tab title={item.name} key={item.id} />
        ))}
      </Tabs>
      <div className="right">
        <Icon type="iconbtn_search" onClick={() => history.push('/search')} />
        <Icon type="iconbtn_channel" onClick={() => setVisible(true)} />
      </div>
      <Popup visible={visible} position="left" bodyClassName={styles.channelPopup}>
        <div className="channel-header">
          <Icon type="iconbtn_channel_close" onClick={handleClose} />
        </div>

        <div className="channel-content">
          <div className="channel-item edit">
            <div className="channel-item-header">
              <span className="channel-item-title">我的频道</span>
              <span className="channel-item-title-extra">点击删除频道</span>

              <span className="channel-item-edit" onClick={() => setEditable(!editable)}>
                {editable ? '完成' : '编辑'}
              </span>
            </div>

            <div className="channel-list">
              {userList.map((item) => {
                return (
                  <span className="channel-list-item" key={item.id}>
                    {item.name}
                    {editable && (
                      <Icon onClick={() => handleDelChannel(item.id)} type="iconbtn_tag_close" />
                    )}
                  </span>
                );
              })}
            </div>
          </div>

          <div className="channel-item">
            <div className="channel-item-header">
              <span className="channel-item-title">频道推荐</span>
              <span className="channel-item-title-extra">点击添加频道</span>
            </div>
            <div className="channel-list">
              {restList.map((item) => {
                return (
                  <span
                    key={item.id}
                    className="channel-list-item"
                    onClick={() => hanelSetChannel(item)}
                  >
                    {item.name}
                  </span>
                );
              })}
            </div>
          </div>
        </div>
      </Popup>
    </div>
  );
}
